﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CallCenter.Data;
using CallCenter.Data.BizUtilities;
using System.Configuration;
using System.Data;
using CallCenter.Security;
using CallCenter.Data.Models;
using CallCenter.Data.Misc;

namespace CallCenter.Web.Controllers
{
    [AuthorizeCore]
    public class AddCompanyController : BaseController
    {
        // GET: AddCompany
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CompanyHistory()
        {
            CompanyGridViewModel cmpy = new CompanyGridViewModel();
            return View(cmpy);
        }
        public JsonResult GetAddCompany()
        {
            if (User.Identity.IsAuthenticated)
            {
                var user = this.Bridge.AccountUtility.GetUser(User.Identity.Name);
            }
            var scratchPads = this.Bridge.CompanyUtility.GetAll();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetCompanyByLoggedInUser()
        {
            if (User.Identity.IsAuthenticated)
            {
                var scratchPads = this.Bridge.CompanyUtility.GetCompanyByLoggedInUser(User.Identity.Name);
                return Json(scratchPads, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetStates()
        {
            var scratchPads = this.Bridge.CompanyUtility.GetStates();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCompanyType()
        {
            var scratchPads = this.Bridge.CompanyUtility.GetCompanyType();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetClientType()
        {
            var scratchPads = this.Bridge.CompanyUtility.GetClientType();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult CreateAddCompany(CompanyGridViewModel model)
        {
            var returnMessage = this.Bridge.CompanyUtility.InsertToCompany(model);
            if (!returnMessage.Status)
                ModelState.AddModelError("Error", returnMessage.Message);
            else 
                return Json(new AjaxSuccess { Message = "Success" }, JsonRequestBehavior.AllowGet);

            return Json(new AjaxFailure { Errors = ModelState.SerializeErrors }, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult DeleteAddCompany(CompanyGridViewModel model)
        {
           
            int id = model.id;
            this.Bridge.CompanyUtility.Delete(id);
            var returnMessage = "Success";
            return Json(returnMessage, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateAddCompany(CompanyGridViewModel model)
        {
            var returnMessage = this.Bridge.CompanyUtility.UpdateToCompany(model);
            
            if (!returnMessage.Status)
                ModelState.AddModelError("Error", returnMessage.Message);
            else
                return Json(new AjaxSuccess { Message = "Success" }, JsonRequestBehavior.AllowGet);
            
            return Json(new AjaxFailure { Errors = ModelState.SerializeErrors }, JsonRequestBehavior.DenyGet);
          //  return Json(result, JsonRequestBehavior.AllowGet);
        }


    }
}

